from interface_frame.base import Base
from data_file_analysis.yaml_analysis import YamlAnalysis as ya

#业务代码重写父类方法
class BO(Base):

    _token = None

    def __init__(self, url, token):
        super(BO, self).__init__(url)
        self._token = token
        self.log.logger.info(f"token:{self._token}")

    def http_request(self, file_path, api_action, params):

        self.log.logger.info(f'{api_action}:')
        api_info = ya.return_yaml_data(file_path, api_action)

        # 日志打印
        self.log.logger.info(f'method:{api_info["method"]}, url:{api_info["url"]}')
        self.log.logger.info(f"params:{params}")

        url = self._url + f"{api_info['url']}?access_token={self._token}"
        method = api_info['method']

        if method in ["get", "GET", "Get"]:
            url = f'{url}&{params}'
            self.get_request(url)

        elif method in ["post", "POST", "Post"]:
            self.post_request(url, data=params)

        else:
            raise Exception(f'method:{method} is wrong!')

        self.log.logger.info(self.re.json())
        return self.re.json()
